// pages/AddAddress/index.js
// 后端配置
import {
  BasicUrl,
  request
} from "../../api/config.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BasicUrl:'',
    isEdit:false,
    from: {
      name: '',
      phone: "",
      // 标签 公司：1，家：2，学校：3
      tag: 1,
      sex: "1",
      detailedAddress: '',
      Address: '',
    }
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      BasicUrl:BasicUrl
    })
    const eventChannel = this.getOpenerEventChannel()
    if(eventChannel){
      let that = this
      // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
      eventChannel.on('get_info', function(data) {
       console.log(data)
       let from = {
         name: data.data.contacts,
         phone: data.data.phone,
         // 标签 公司：1，家：2，学校：3
         tag: data.data.tag,
         sex:data.data.sex,
         detailedAddress: data.data.detailed_address,
         Address: data.data.address,
         id:data.data.id
       }
       that.setData({
         from,
         isEdit:true
       })
     })
    }
  },
  oneClickAccess() {
    let that = this
    wx.chooseAddress({
      success(res) {
        console.log(res);
        that.setData({
          'from.Address': res.provinceName + res.cityName + res.countyName,
          'from.detailedAddress': res.detailInfo,
          'from.phone': res.telNumber,
          'from.name': res.userName
        })
      }
    })
  },
  selectMapAddress() {
    let that = this
    wx.chooseAddress({
      success(res) {
        console.log(res);
        that.setData({
          'from.Address': res.provinceName + res.cityName + res.countyName,
          'from.detailedAddress': res.detailInfo
        })
      }
    })
  },
  save() {
    console.log(this.data.from);
    let obj = this.data.from
    let validate = true
    for (let key in obj) {
      if (obj.hasOwnProperty(key) && !obj[key]) { // 如果key存在于obj中，且其值为falsy  
        validate =  false;
      }
    }
    if(!validate){
      return wx.showToast({
        title: '请完善信息',
        icon:'none'
      })
    }else{
      let user_id = JSON.parse(wx.getStorageSync('user_info')).user_id
      let query = {
        ...this.data.from,
        user_id:user_id
      }
      console.log(query);
      let api = this.data.isEdit ? '/api/receiving_address/update' :'/api/receiving_address/add'
      request(api,'post',query,true).then(({data})=>{
        console.log(data);
        if(data.code == 200){
          wx.showToast({
            title: '保存成功',
          })
          wx.navigateTo({
            url: '/pages/myAddress/index',
          })
        }else{
          wx.showToast({
            title: '保存失败',
          })
        }
      })
    }
  },
  getdetailedAddress(e) {
    this.setData({
      'from.detailedAddress': e.detail
    })
  },
  getphone(e) {
    this.setData({
      'from.phone': e.detail
    })
  },
  getsex(e) {
    this.setData({
      'from.sex': e.detail
    })
  },
  getusername(e) {
    console.log(e);
    let value = e.type == 'blur' ? e.detail.value : e.detail
    this.setData({
      'from.name':value
    })
  },
  select_tag(e) {
    let idx = e.currentTarget.dataset.idx
    this.setData({
      'from.tag': idx
    })
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  // 自动填写
  autoFill() {
    let phone = JSON.parse(wx.getStorageSync('user_info')).phone
    console.log(phone);
    if (phone > 0) {
      this.setData({
        'from.phone': phone
      })
    } else {
      wx.showToast({
        title: '请完善手机号',
        icon: 'error'
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})